package com.shujia.flink.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

public class Demo03FileSource {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 老版读文件的方式：有界流
        DataStreamSource<String> fileDS = env.readTextFile("flink/data/words.txt");

//        fileDS.print();

        // 新版读文件的方式：FileSource
        FileSource<String> fileSource =
                FileSource.forRecordStreamFormat(
                                new TextLineInputFormat("utf8") // 格式化类
                                , new Path("flink/data/words") // 路径
                        )
                        .monitorContinuously(Duration.ofSeconds(5)) // 指定时间间隔监控目录下的文件变更：将有界流转换为无界流
                        .build();

        // 通过env注册Source
        DataStreamSource<String> newFileDS = env.fromSource(fileSource, WatermarkStrategy.noWatermarks(), "fileSource");
        newFileDS.print();

        env.execute();
    }
}
